//
//  YHTabBarItem.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/26.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHTabBarItem: UITabBarItem {

   override var badgeValue: String? {
    didSet{
        let target = valueForKey("_target") as! YHMainViewController
        
        for subview in target.tabBar.subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                for v in subview.subviews {
                    if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                        for bgView in v.subviews {
                            if bgView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                var count: UInt32 = 0
                                let list = class_copyIvarList(NSClassFromString("_UIBadgeBackground").self, &count)
                                for i in 0..<count {
                                    let ivarPoint = list[Int(i)]
                                    let ivarName = ivar_getName(ivarPoint)
                                    print(ivarName)
                                    let name = String(format: "%s",ivarName)
                                    let type = ivar_getTypeEncoding(ivarPoint)
                                    let typeName = String(format:"%s",type)
                                    print(name,typeName)
                                    let image = UIImage(named: "main_badge")
                                    bgView.setValue(image, forKey: "_image")
                                    
                                }
                                
                            }
                        }
                    }
                }
            }
            
        }

    }
        
    }
}
